﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing.Design;

namespace OntologyFormDesigner.GuiComponent
{
    public partial class OntoToolbar : Panel, IToolboxService
    {
        internal Control designPanel = null;
        

        public OntoToolbar()
        {
            InitializeComponent();
        }
        
        public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator, string format, System.ComponentModel.Design.IDesignerHost host)
        {
            // No implementation
        }

        public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator, string format)
        {
            // No implementation
        }

        public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category, System.ComponentModel.Design.IDesignerHost host)
        {
            // No implementation
        }

        public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, System.ComponentModel.Design.IDesignerHost host)
        {
            // No implementation
        }

        public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category)
        {
            AddToolboxItem(toolboxItem);
        }

        public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            //all
            for (int i = 0; i < tabControl.TabPages[0].Controls.Count; i++)
                if (tabControl.TabPages[0].Controls[i] is ListBox)
                    ((ListBox)tabControl.TabPages[0].Controls[i]).Items.Add(toolboxItem);
            //or
            Console.WriteLine("toolboxItem: " + toolboxItem.TypeName);
         
            try
            {
                foreach (Type type in Type.GetType(toolboxItem.TypeName).GetInterfaces())
                {
                    if (type.FullName.Equals("OntologyUtility.Interfaces.IOrWidget"))
                    {
                        for (int i = 0; i < tabControl.TabPages[IndexTabOr].Controls.Count; i++)
                            if (tabControl.TabPages[IndexTabOr].Controls[i] is ListBox)
                                ((ListBox)tabControl.TabPages[IndexTabOr].Controls[i]).Items.Add(toolboxItem);
                    }
                    else if (type.FullName.Equals("OntologyUtility.Interfaces.IAlternativeWidget"))
                    {
                        for (int i = 0; i < tabControl.TabPages[IndexTabAlternative].Controls.Count; i++)
                            if (tabControl.TabPages[IndexTabAlternative].Controls[i] is ListBox)
                                ((ListBox)tabControl.TabPages[IndexTabAlternative].Controls[i]).Items.Add(toolboxItem);
                    }
                    else if (type.FullName.Equals("OntologyUtility.Interfaces.IViewerWidget"))
                    {
                        for (int i = 0; i < tabControl.TabPages[IndexTabViewer].Controls.Count; i++)
                            if (tabControl.TabPages[IndexTabViewer].Controls[i] is ListBox)
                                ((ListBox)tabControl.TabPages[IndexTabViewer].Controls[i]).Items.Add(toolboxItem);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("AddToolboxItem: " + e.ToString());
            }
            Console.WriteLine("\n\n");
            //((ListBox)tabControl.TabPages.).Items.Add(toolboxItem);
            //listBox.Items.Add(toolboxItem);
        }

        public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            return null;
        }

        public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject)
        {
            return null;
        }

        public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem(System.ComponentModel.Design.IDesignerHost host)
        {
            return GetSelectedToolboxItem();
        }

        public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem()
        {
            if (((ListBox)tabControl.SelectedTab.Controls[0]).SelectedIndex == -1)
                return null;
            else
                return (ToolboxItem)((ListBox)tabControl.SelectedTab.Controls[0]).SelectedItem;
        }

        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(string category, System.ComponentModel.Design.IDesignerHost host)
        {
            return GetToolboxItems();
        }

        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(string category)
        {
            return GetToolboxItems();
        }

        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(System.ComponentModel.Design.IDesignerHost host)
        {
            return GetToolboxItems();
        }

        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems()
        {
            ToolboxItem[] t = new ToolboxItem[((ListBox)tabControl.SelectedTab.Controls[0]).Items.Count];
            ((ListBox)tabControl.SelectedTab.Controls[0]).Items.CopyTo(t, 0);

            return new ToolboxItemCollection(t);
        }

        public bool IsSupported(object serializedObject, System.Collections.ICollection filterAttributes)
        {
            return false;
        }

        public bool IsSupported(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            return false;
        }

        public bool IsToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            return false;
        }

        public bool IsToolboxItem(object serializedObject)
        {
            return false;
        }

        //		public void Refresh()
        //		{
        //			base.Refresh();
        //		}

        public void RemoveCreator(string format, System.ComponentModel.Design.IDesignerHost host)
        {
            // No implementation
        }

        public void RemoveCreator(string format)
        {
            // No implementation
        }

        public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category)
        {
            RemoveToolboxItem(toolboxItem);
        }

        public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            ((ListBox)tabControl.SelectedTab.Controls[0]).Items.Remove(toolboxItem);
        }

        public void SelectedToolboxItemUsed()
        {
            ((ListBox)tabControl.SelectedTab.Controls[0]).SelectedIndex = -1;
        }

        public object SerializeToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            return null;
        }

        public bool SetCursor()
        {
            for (int j = 0; j < tabControl.TabPages.Count; j ++)
                for (int i = 0; i < tabControl.TabPages[j].Controls.Count; i++)
                    if (tabControl.TabPages[j].Controls[i] is ListBox)
                    {
                        if (((ListBox)tabControl.TabPages[j].Controls[i]).SelectedIndex == -1)
                            designPanel.Cursor = Cursors.Default;
                        else
                            designPanel.Cursor = Cursors.Cross;
                    }

            return true;
        }

        public void SetSelectedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            ((ListBox)tabControl.SelectedTab.Controls[0]).SelectedItem = toolboxItem;
        }

        public System.Drawing.Design.CategoryNameCollection CategoryNames
        {
            get
            {
                return null;
            }
        }

        public string SelectedCategory
        {
            get
            {
                return null;
            }
            set
            {
            }
        }

        private bool ShouldSerializeItems()
        {
            return false;
        }
    }
}
